package morenoapp.tools;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import morenoapp.components.common.ExceptionDialog;
import morenoapp.util.Order;

/**
 *
 * @author root
 */
public class GenerateComponentsInitialisation {
    public static void main(String[] args) {
        List<Field> fields = new ArrayList<Field>(Arrays.asList(ExceptionDialog.class.getDeclaredFields()));
        Collections.sort(fields, new Comparator<Field>() {
            @Override
            public int compare(Field o1, Field o2) {
                return -(nvl(o2.getAnnotation(Order.class)) - nvl(o1.getAnnotation(Order.class)));
            }

            int nvl(Order order) {
                if (order == null) {
                    return 0;
                } else {
                    return order.value();
                }
            }

        });
        for (Field f : fields) {
            //System.out.println(f.getName());
            if (JComponent.class.isAssignableFrom(f.getType())) {
                String shortName = f.getType().getSimpleName().toLowerCase();
                if (AbstractButton.class.isAssignableFrom(f.getType())) {
                    shortName = "button";
                }
                if (JMenu.class.isAssignableFrom(f.getType())) {
                    shortName = "menu";
                }
                if (JMenuItem.class.isAssignableFrom(f.getType())) {
                    shortName = "item";
                }
                if (JTextField.class.isAssignableFrom(f.getType())) {
                    shortName = "text";
                }

                System.out.printf(
                        "{ //%s\n" +
                        "%s %s = new %s();\n" +
                        "%s.setName(\"%s\");\n", f.getName(), f.getType().getSimpleName(), shortName, f.getType().getSimpleName(), shortName, f.getName());
                if (AbstractButton.class.isAssignableFrom(f.getType())) {
                    System.out.printf("%s.setAction(MorenoActions.get);\n", shortName);
                }
                System.out.printf("%s = %s;\n" +
                        "}\n",
                        f.getName(), shortName);
            }
        }
    }

}
